package com.tian.web.admin.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.oscache.util.StringUtil;
import com.tian.core.action.JqGridBaseAction;
import com.tian.core.bean.Condition;
import com.tian.core.bean.ConditionOperate;
import com.tian.core.bean.ConditionType;
import com.tian.core.bean.Pages;
import com.tian.core.entity.Group;
import com.tian.core.entity.Master;
import com.tian.web.admin.service.GroupService;
@Namespace("/admin")
@Results( {  
	@Result(type = "dispatcher",name="group",location="/pages/admin/group_list.jsp"),
})
public class GroupAction extends JqGridBaseAction{

	private static final long serialVersionUID = 6319318422268607600L;

	private List<Group> gridModel;
	
	private Group group;
	
	@Resource
	private GroupService groupService;
	
	@Override
	public String refreshGridModel() {
		this.record = groupService.getTotalCount(Group.class);
		Pages pages = new Pages();		
    	pages.setPage(page);
    	pages.setTotalNum(record);
    	pages.setPerPageNum(rows);
    	pages.executeCount();
    	total = pages.getAllPage();
    	
    	if(!StringUtil.isEmpty(this.sidx) && !StringUtil.isEmpty(this.sord)){
    		Condition condition1 = new Condition();
        	condition1.setName(this.sidx);
    		//condition1.setValue(Condition.DESC);
    		condition1.setValue(this.sord);
    		condition1.setType(ConditionType.ORDER);
    		conditions.add(condition1);
    	}
    	//只实现equal
    	if(this.search && !StringUtil.isEmpty(this.searchField)
    			&& !StringUtil.isEmpty(this.searchOper)&& !StringUtil.isEmpty(this.searchString)){
    		Condition condition2 = new Condition();
    		condition2.setName(this.searchField);
    		//condition1.setValue(Condition.DESC);
    		condition2.setValue(this.searchString);
    		condition2.setType(ConditionType.PROPERTY);
    		condition2.setOperate(ConditionOperate.EQUAL);
    		conditions.add(condition2);
    	}
		
    	pageList = groupService.findByPages(conditions, pages);
		pages = pageList.getPages();
		
        this.rows = pages.getPerPageNum();
        this.record = pages.getTotalNum();
        this.page = pages.getPage();
        
        gridModel = (List<Group>)pageList.getObjectList();
        //设为null，否则无法转换为json对象
/*		for(Group m :gridModel){
			m.setActiongroups(null);
			m.setMastergroups(null);
			m.setZtrees(null);
		}*/
		System.out.println("rows:"+rows+" page:"+ page + " total:" + total + " record:" + record);
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		return "group";
	}
	
	public String list() throws Exception {
		
		return this.refreshGridModel();
	}	
	
	public String delete() throws Exception {
		if(StringUtil.isEmpty(this.id)){
    		return ERROR;
    	}
		groupService.delete(Integer.parseInt(id));
		return ajax("ajax_ok");
	}

    public String addOrUpdate() throws Exception {
    	if(id!=null){//编辑
    		group = groupService.get(Integer.parseInt(id));
    	}else{
    		group = new Group();
    	}
    	return "update";
    }
    
    public String save() throws Exception {
    	
    	if(group.getId()!=null){//编辑
    		Group g = groupService.get(group.getId());
    		g.setGroupname(group.getGroupname());
    		g.setGroupinfo(group.getGroupinfo());
    		groupService.update(g);
    		
    	}else{//新增
    		group.setCreatedate(new Date());
    		//获取session中用户
			Master m0 = (Master)ServletActionContext.getRequest().getSession().getAttribute("user");
			group.setMasterid(m0.getId());
			group.setMastername(m0.getName());
			groupService.save(group);
    	}
    	return "group";
    }
	
	public List<Group> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Group> gridModel) {
		this.gridModel = gridModel;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

}
